package com.culture.config.core;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.culture.config.common.util.HostAddressUtil;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;

import com.culture.config.service.InstanceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api
@Slf4j
@RestController
@RequestMapping({"/instances"})
public class RegisterController {
    @Autowired
	private InstanceService instanceService;
    
    @ApiOperation("instances register")
    @PostMapping("")
    public RegistrationVO register(@RequestBody @Valid RegistrationPO param,HttpServletRequest request){           
        param.setServiceIp(HostAddressUtil.getClientIpAddress(request));
        param.setSessionId(request.getSession().getId());
        log.info("register info:{}",param);
        return this.instanceService.register(param);
    }

    @ApiOperation("instances unregister")
    @DeleteMapping(path = "/{id}")
    public void unregister(@PathVariable String id) {
        log.info("unregister id:{}",id);
        this.instanceService.unRegister(id);
    }

    @ApiOperation("instances heartbeat")
    @PostMapping(path = "/heartbeat/{id}")
    public void heartbeat(@PathVariable String id) {
        log.info("heartbeat id:{}",id);
        this.instanceService.heartbeat(id);
    }
}
